<template>
  <div class="container">
    <el-card class="card">
      <el-row type="flex" justify="space-between" style="margin-bottom: 15px">
        <el-col
          :span="12"
        ><div class="grid-content bg-purple">
          说明：目前支持学科和关键字条件筛选
        </div></el-col>
        <el-col
          :span="1"
        ><div class="grid-content bg-purple-light">
          <el-button
            type="success"
            size="small"
            @click="$router.push('/questions/new')"
          >
            <i class="el-icon-edit" />
            新增试题</el-button>
        </div></el-col>
      </el-row>

      <!-- 筛选 -->
      <Screening @handleSearch="handleSearch" />

      <el-tabs
        v-loading="loading"
        v-model="activeName"
        type="card"
        @tab-click="handleClick"
      >
        <el-tab-pane label="全部" name="first">
          <el-alert :title="alertTitle" show-icon type="info" />
          <QuestionsChoiceTable
            @delSubject="delSubject"
            :tableData="tableData"
            :total="total"
            @getTableData="getChoiceList"
          />
        </el-tab-pane>
        <el-tab-pane label="待审核" name="second">
          <el-alert :title="alertTitle" type="info" />
          <QuestionsChoiceTable
            @delSubject="delSubject"
            :tableData="tableData"
            :total="total"
            @getTableData="getChoiceList"
          />
        </el-tab-pane>
        <el-tab-pane label="已审核" name="third">
          <el-alert :title="alertTitle" type="info" />
          <QuestionsChoiceTable
            @delSubject="delSubject"
            :tableData="tableData"
            :total="total"
            @getTableData="getChoiceList"
          />
        </el-tab-pane>
        <el-tab-pane label="已拒绝" name="fourth">
          <el-alert :title="alertTitle" type="info" />
          <QuestionsChoiceTable
            @delSubject="delSubject"
            :tableData="tableData"
            :total="total"
            @getTableData="getChoiceList"
          />
        </el-tab-pane>
      </el-tabs>
      <!-- 分页 -->
      <el-pagination
        class="pagination"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[1, 3, 5, 10, 20]"
        :page-size="pagesize"
        layout="prev, pager, next, sizes, jumper"
        :total="total"
      />
    </el-card>
  </div>
</template>

<script>
import { choice } from '@/api/hmmm/questions'
import Screening from './components/screening.vue'
import QuestionsChoiceTable from './components/questions-choice-table.vue'
// import { chkType } from '@/api/hmmm/constants.js'
export default {
  name: 'questions-choice',
  components: {
    Screening,
    QuestionsChoiceTable
  },
  data () {
    return {
      activeName: 'first',
      page: 1,
      pagesize: 5,
      tableData: [],
      total: 0,
      chkState: '',
      loading: false
    }
  },
  computed: {
    alertTitle () {
      return `共 ${this.total} 条数据`
    }
  },
  created () {
    this.getChoiceList()
  },
  methods: {
    handleClick (tab, event) {
      // console.log(tab.label, '111')
      // console.log(event, '222')
      this.page = 1
      this.pagesize = 5
      // const res = chkType.find(item => item.label === tab.label)
      if (tab.label === '全部') {
        this.getChoiceList()
      }
      if (tab.label === '待审核') {
        this.getChoiceList('', 0)
      }
      if (tab.label === '已审核') {
        this.getChoiceList('', 1)
      }
      if (tab.label === '已拒绝') {
        this.getChoiceList('', 2)
      }
      // console.log(tab)
      // this.getChoiceList()
    },
    handleSearch (form) {
      console.log(form)
      this.page = 1
      this.pagesize = 5
      this.getChoiceList(form)
    },
    // 查询精选题库列表
    async getChoiceList (form = '', chkState) {
      this.loading = true
      const res = await choice({
        page: this.page,
        pagesize: this.pagesize,
        ...form,
        chkState
      })
      this.tableData = res.data.items
      this.total = res.data.counts
      this.loading = false
    },
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.pagesize = val
      this.page = 1
      this.getChoiceList()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.page = val
      this.getChoiceList()
    },
    delSubject () {
      if (this.tableData.length === 1 && this.page > 1) {
        this.page--
      }
      this.getChoiceList()
    }
  }
}
</script>

<style scoped lang='scss'>
.card {
  margin: 5px 15px;
  padding: 0 10px;
}
.bg-purple {
  color: pink;
  font-size: 12px;
}
.pagination {
  float: right;
  margin: 20px 0;
}
</style>
